package com.yic.module.device.controller.admin.inspectionPlan;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.security.core.util.SecurityFrameworkUtils;
import com.yic.module.device.controller.admin.device.vo.DeviceSimpleRespVO;
import com.yic.module.device.controller.admin.inspectionPlanDevice.vo.InspectionPlanDeviceExportReqVO;
import com.yic.module.device.dal.dataobject.device.DeviceDO;
import com.yic.module.device.dal.dataobject.inspectionPlanDevice.InspectionPlanDeviceDO;
import com.yic.module.device.service.device.DeviceService;
import com.yic.module.device.service.inspectionPlanDevice.InspectionPlanDeviceService;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionPlan.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlan.InspectionPlanDO;
import com.yic.module.device.convert.inspectionPlan.InspectionPlanConvert;
import com.yic.module.device.service.inspectionPlan.InspectionPlanService;

@Tag(name = "管理后台 - 巡检计划")
@RestController
@RequestMapping("/device/inspection-plan")
@Validated
public class InspectionPlanController {

    @Resource
    private InspectionPlanService inspectionPlanService;
    @Resource
    private InspectionPlanDeviceService inspectionPlanDeviceService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建巡检计划")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:create')")
    public CommonResult<Long> createInspectionPlan(@Valid @RequestBody InspectionPlanCreateReqVO createReqVO) {
        return success(inspectionPlanService.createInspectionPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新巡检计划")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:update')")
    public CommonResult<Boolean> updateInspectionPlan(@Valid @RequestBody InspectionPlanUpdateReqVO updateReqVO) {
        inspectionPlanService.updateInspectionPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除巡检计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:delete')")
    public CommonResult<Boolean> deleteInspectionPlan(@RequestParam("id") Long id) {
        inspectionPlanService.deleteInspectionPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得巡检计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:query')")
    public CommonResult<InspectionPlanRespVO> getInspectionPlan(@RequestParam("id") Long id) {
        InspectionPlanDO inspectionPlan = inspectionPlanService.getInspectionPlan(id);
        InspectionPlanRespVO result = InspectionPlanConvert.INSTANCE.convert(inspectionPlan);
        // 获得巡检计划绑定的设备信息
        List<InspectionPlanDeviceDO> inspectionPlanDeviceDOList = inspectionPlanDeviceService.selectListByPlanId(id);
        List<Long> deviceIdList = inspectionPlanDeviceDOList.stream().map(InspectionPlanDeviceDO::getDeviceId).collect(Collectors.toList());
        // 获得设备信息
        List<DeviceDO> deviceDOList = deviceService.getList(deviceIdList);
        List<DeviceSimpleRespVO> deviceSimpleRespVOList = InspectionPlanConvert.INSTANCE.convert03(deviceDOList);
        result.setDeviceList(deviceSimpleRespVOList);
        return success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获得巡检计划列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:query')")
    public CommonResult<List<InspectionPlanRespVO>> getInspectionPlanList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectionPlanDO> list = inspectionPlanService.getInspectionPlanList(ids);
        return success(InspectionPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得巡检计划分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:query')")
    public CommonResult<PageResult<InspectionPlanRespVO>> getInspectionPlanPage(@Valid InspectionPlanPageReqVO pageVO) {
        PageResult<InspectionPlanDO> pageResult = inspectionPlanService.getInspectionPlanPage(pageVO);
        PageResult<InspectionPlanRespVO> result = InspectionPlanConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item->{
            // 封装计划人员
            AdminUserRespDTO user = adminUserApi.getUser(item.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                item.setNickname(user.getNickname());
            }
        });
        return success(result);
    }


    @GetMapping("/app-page")
    @Operation(summary = "移动端获得巡检计划分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:query')")
    public CommonResult<PageResult<InspectionPlanRespVO>> getInspectionPlanAppPage(@Valid InspectionPlanPageReqVO pageVO) {
        AdminUserRespDTO loginUser = adminUserApi.getUser(SecurityFrameworkUtils.getLoginUserId());
        pageVO.setUserId(loginUser.getId());
        PageResult<InspectionPlanDO> pageResult = inspectionPlanService.getInspectionPlanAppPage(pageVO);
        PageResult<InspectionPlanRespVO> result = InspectionPlanConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item->{
            // 封装计划人员
            AdminUserRespDTO user = adminUserApi.getUser(item.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                item.setNickname(user.getNickname());
            }
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出巡检计划 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionPlanExcel(@Valid InspectionPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionPlanDO> list = inspectionPlanService.getInspectionPlanList(exportReqVO);
        // 导出 Excel
        List<InspectionPlanExcelVO> datas = InspectionPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检计划.xls", "数据", InspectionPlanExcelVO.class, datas);
    }

}
